<template>

	<view class="view-content">

		<!-- <image class="phone-bg" src="../../../static/image/mine/phoneBg.png" mode=""></image> -->
		<p class="use-text">欢迎使用</p>
		<p class="title-text">建筑产业工人服务平台</p>
		<view class="img-div">
			<image class="head-img" :src="personInfo.avatarUrl?personInfo.avatarUrl:require('@/static/image/mine/head-img.png')"></image>
		</view>
		<button class="auth-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">微信一键登录</button>
	</view>
</template>

<script>
	import request from '@/static/js/request.js'
	export default {
		components: {

		},
		data() {
			return {
				personInfo: ''
			}
		},
		onShow() {
			this.personInfo = JSON.parse(uni.getStorageSync('userInfo'));
		},
		onUnload(){
			if(uni.getStorageSync('userInfo')){
				this.personInfo = JSON.parse(uni.getStorageSync('userInfo'));
				if(this.personInfo && this.personInfo.uid){
					return
				}else{
					if(this.personInfo&&this.personInfo.account_type==''){
						uni.clearStorageSync();
						uni.switchTab({
							url: "/pages/main/mine"
						})
					}
				}
			}
		},
		methods: {
			onGetPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					uni.login({
						success: (loginRes) => {
							var code = loginRes.code;
							let data = {
								code,
								encryptedData: e.detail.encryptedData,
								iv: e.detail.iv,
							};
							request.post('/wmp', data).then(resp => {
								uni.setStorageSync('isphone', 'true')
								this.phoneLogin(resp.data.phoneNumber)
							}).catch(e => {
								uni.showToast({
									icon: 'none',
									title: e.message
								})
							})
						}
					})
				}
			},
			phoneLogin(phoneNum) {
				let data = {
					avatarUrl:this.personInfo.avatarUrl,
					openid:this.personInfo.openid,
					city:this.personInfo.city,
					province:this.personInfo.province,
					gender:	this.personInfo.gender,   
					mobile: phoneNum,
					name: this.personInfo.nickName,
					vmp:'vmp'
				}
				uni.showLoading({
					title: '正在登录'
				})
				request.post('/api/v1/signup/0', data).then(resp => {
					// console.log(resp.data,'88888')
					resp.data.avatarUrl = this.personInfo.avatarUrl
					uni.setStorageSync('userInfo', JSON.stringify(resp.data))
					var account_type = JSON.parse(uni.getStorageSync('userInfo')).account_type
					uni.setStorageSync('closeWebsocket', true)
					if (resp.data.is_confirm === 'False' && account_type === '') {
						uni.hideLoading();
						uni.navigateTo({
							url: '/pages/main/mine/MineRegisterIdentity'
						})
					}else{
						uni.hideLoading();
						uni.switchTab({
							url: '/pages/main/mine'
						})
					}
				}).catch(e => {
					console.log(e)
					uni.showToast({
						icon: 'none',
						title: e.message
					})
				})
			}
		}
	}
</script>


</script>

<style scoped lang="less">
	.view-content {
		width: 100%;
		height: 100vh;
		position: relative;
		overflow: hidden;

		.phone-bg {
			position: absolute;
			top: 0;
		}

		.use-text {
			font-size: 40rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #666666;
			line-height: 42rpx;
			margin: 60rpx 40rpx;
		}

		.title-text {
			font-size: 60rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
			line-height: 72rpx;
			margin-left: 40rpx;
		}

		.img-div {
			width: 200rpx;
			height: 200rpx;
			margin: 20% 38%;

			.head-img {
				width: 200rpx;
				height: 200rpx;
				border-radius: 50%;
				border: 1px solid;
			}
		}

		button::after {
			border: none;
		}

		.auth-btn {
			color: #FFFFFF;
			font-size: 32rpx;
			text-align: center;
			background-color: #11D36A;
			line-height: 40rpx;
			width: 90%;
			padding: 20rpx 0;
		}
	}
</style>
